﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Acms.DataControls;
using Acms.AcmsControls.ContentControls.FrontEnd;

public partial class Templates_TradeShowHolder_Default : Acms.AcmsControls.TemplatePage
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);        
        dsSibling.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsSibling_SelectData);

        dsList.SelectData += new EventHandler<SelectEventArgs<Post>>(dsList_SelectData);
        dlFeed.ItemDataBound += new EventHandler<Acms.WebControls.ItemDataBoundEventArgs>(dlFeed_ItemDataBound); 
        dlNavSiblings.ItemDataBound += new EventHandler<Acms.WebControls.ItemDataBoundEventArgs>(dlNavSiblings_ItemDataBound);
    }

    void dlFeed_ItemDataBound(object sender, Acms.WebControls.ItemDataBoundEventArgs e)
    {
        var post = e.DataItem as Post;
        if (post != null)
        {

            //lnkWebsite
            var lnkWebsite = e.Item.FindControl("lnkWebsite") as HyperLink;
            if (lnkWebsite != null)
            {
                var website = post.FindPostControl("Website");
                var websiteDisplayName = post.FindPostControl("WebsiteDisplayName");
                if (website != null && websiteDisplayName !=null)
                {
                    var websiteProp = website.GetProperties<Acms.AcmsControls.TextContentControlProperties>();
                    var websiteDisplayNameProp = websiteDisplayName.GetProperties<Acms.AcmsControls.TextContentControlProperties>();
                    
                    if (websiteProp != null)
                    {
                        String propUrl = websiteProp.Text + "..";
                        String httpTest = propUrl.Substring(0, 3);

                        bool addHttp = String.Compare(propUrl.Substring(0, 3), "www") == 0; // This is true.
                        if (addHttp == true) {
                           propUrl = "http://" + propUrl;
                        }
                        lnkWebsite.NavigateUrl = propUrl;
                        
                        if (websiteDisplayNameProp != null) {
                            String websiteDisplayNameText = websiteDisplayNameProp.Text;
                            lnkWebsite.Text = websiteDisplayNameText; //websiteDisplayNameProp.Text;//prop.Text;
                        }
                    }
                    else
                    {
                        lnkWebsite.Visible = false;
                    }
                }
            }

            //lblUnmannedAirSystem
            var lblUnmannedAirSystem = e.Item.FindControl("lblUnmannedAirSystem") as Label;
            if (lblUnmannedAirSystem != null)
            {
                var tfUnmannedAirSystem = post.FindPostControl("UnmannedAirSystem");
                if (tfUnmannedAirSystem != null) {
                    var prop = tfUnmannedAirSystem.GetProperties<Acms.AcmsControls.TrueFalseContentControlProperties>();
                    if (prop != null) { 
                        if (prop.Value == true)
                        {
                            lblUnmannedAirSystem.Visible = true;
                        }
                        else
                        {
                            lblUnmannedAirSystem.Visible = false; ;
                        }
                    }
                }
            }
            //lblMineDetectionSystems
            var lblMineDetectionSystems = e.Item.FindControl("lblMineDetectionSystems") as Label;
            if (lblMineDetectionSystems != null)
            {
                var tfMineDetectionSystems = post.FindPostControl("MineDetectionSystems");
                if (tfMineDetectionSystems != null)
                {
                    var prop = tfMineDetectionSystems.GetProperties<Acms.AcmsControls.TrueFalseContentControlProperties>();
                    if (prop != null)
                    {
                        if (prop.Value == true)
                        {
                            lblMineDetectionSystems.Visible = true;
                        }
                        else
                        {
                            lblMineDetectionSystems.Visible = false; ;
                        }
                    }
                }
            }
            //lblMineDetectionSystems
            var lblCompositeTechnologies = e.Item.FindControl("lblCompositeTechnologies") as Label;
            if (lblMineDetectionSystems != null)
            {
                var tfCompositeTechnologies = post.FindPostControl("CompositeTechnologies");
                if (tfCompositeTechnologies != null)
                {
                    var prop = tfCompositeTechnologies.GetProperties<Acms.AcmsControls.TrueFalseContentControlProperties>();
                    if (prop != null)
                    {
                        if (prop.Value == true)
                        {
                            lblCompositeTechnologies.Visible = true;
                        }
                        else
                        {
                            lblCompositeTechnologies.Visible = false; ;
                        }
                    }
                }
            }
        }
    }

    

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBind();       
        }
    }

    void dsList_SelectData(object sender, SelectEventArgs<Post> e)
    {
        e.SetData(AcmsContext.CurrentPost.Children.OrderBy(x => x.Sequence).ToList());
    }

    void dsSibling_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        e.SetData(AcmsContext.CurrentPost.Parent.Children.OrderBy(x => x.Sequence).ToList());
    }
    void dlNavSiblings_ItemDataBound(object sender, Acms.WebControls.ItemDataBoundEventArgs e)
    {
        var post = e.DataItem as Post;
        if (post != null)
        {
            var lnk = e.Item.FindControl("lnkNav") as HyperLink;

            if (lnk != null)
            {
                lnk.Text = post.DisplayName;
                lnk.NavigateUrl = post.Url;
            }
        }
    }
    
    //protected void UnmannedAirSystem_DataBound(object sender, EventArgs e)
    //{
    //    phUnmannedAirSystem.Visible = tfUnmannedAirSystem.Value;
    //}
}
